const conn = require("../../libs/DB/Conn");
const _ = require("underscore");
const {createSql,pieTimeRange} = require("./dataAnalysisUtil");
const moment = require("moment");

module.exports = async function(params){

    let time = pieTimeRange(params.timeType)
    let sql = createSql(params.type,params.userDomainId,time.type,time.range,{type:"range",query:"total"},null,null);

    let result = await conn(sql.sql);
    let dataList = [];
    let rangeGroup = _.groupBy(result,"rangeId");
    for(const rangeId in rangeGroup){
        let rangeData = rangeGroup[rangeId];
        dataList.push({value:rangeData[0].consumption,name:rangeData[0].rangeName})
    }

    return {success:true,dataList:dataList};

}